﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BussinessEntity;
using Equin.ApplicationFramework;
using Utilities;
using BussinessLayer;
using Configurations;

namespace PresentationLayer
{
    public delegate void CapNhatDS(List<ReceiptDetail> ds);

    public partial class frmReceipt : DevComponents.DotNetBar.OfficeForm
    {
        private List<ReceiptDetail> ListCT;
        private bool IsAdd;
        public Receipt HoaDonMoi { get; set; }

        public frmReceipt()
        {
            InitializeComponent();
            dgvBillDetail.BackgroundColor = Color.White;
            ListCT = new List<ReceiptDetail>();
            IsAdd = true;
            btnSubmit.Text = "Thêm";
            txtMaHD.Text = ReceiptManager.NextID();
            btnIn.Visible = false;
        }

        public frmReceipt(string MaHD)
        {
            InitializeComponent();
            HoaDonMoi = ReceiptManager.GetSingle(MaHD).Object;
            ListCT = HoaDonMoi.ListDetails;
            CapNhatDSCT(ListCT);
            IsAdd = false;
            btnSubmit.Text = "Cập nhật";
            txtMaHD.Text = MaHD;
        
        }

        bool CheckValidated()
        {
            if (mtbSDT.Text == "")
            {
                mtbSDT.Focus();
                return false;
            }
            if (txtTenKH.Text == "")
            {
                txtTenKH.Focus();
                return false;
            }
            if (txtTenTB.Text == "")
            {
                txtTenTB.Focus();
                return false;
            }
            if (dtpNgayNhan.Value == DateTime.MinValue)
            {
                dtpNgayNhan.Focus();
                return false;
            }
            return true;
        }

        Receipt GetInformation()
        {
            Receipt hd = new Receipt();
            hd.CustomerAddress = txtDiaChi.Text.Trim();
            hd.ReceiptID = txtMaHD.Text.Trim();
            hd.DeviceDescription = txtMoTaTB.Text.Trim();
            hd.DateReceiced = dtpNgayNhan.Value;
            hd.PhoneNumber = mtbSDT.Text.Trim();
            hd.CustomerName = txtTenKH.Text.Trim();
            hd.DeviceName = txtTenTB.Text.Trim();
            hd.Status = cbTrangThai.Text.Trim();
            hd.ListDetails = ListCT;
            foreach (var l in hd.ListDetails)
                l.Receipt = ReceiptManager.GetSingle(hd.ReceiptID).Object;
            return hd;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValidated())
            {
                Receipt hd = GetInformation();
                if (IsAdd)
                {
                    ActionResult<Receipt> rs = ReceiptManager.Insert(hd);
                    if (rs.Success)
                    {
                        HoaDonMoi = hd;
                        MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                        MessageBox.Show(rs.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    ActionResult<Receipt> rs = ReceiptManager.Update(hd);
                    if (rs.Success)
                    {
                        HoaDonMoi = hd;
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                        MessageBox.Show(rs.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
                MessageBox.Show("Nhập thiếu thông tin!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        private void cậpNhậtChiTiếtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReceiptDetail frm = new frmReceiptDetail(ListCT, new CapNhatDS(CapNhatDSCT));
            frm.ShowDialog();
        }

        void CapNhatDSCT(List<ReceiptDetail> ds)
        {
            dgvBillDetail.AutoGenerateColumns = false;
            dgvBillDetail.DataSource = new BindingListView<ReceiptDetail>(ds);
            txtTongTien.Text = DataUtil.ChangeMoneyToString(ReceiptManager.Total(ListCT));
        }

        private void xóaChiTiếtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvBillDetail.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var ct = ListCT.Find(s => s.ServiceID == DataUtil.ToString(dgvBillDetail.SelectedRows[0].Cells["MaDV"].Value));
                    if (ct != null)
                    {
                        ListCT.Remove(ct);
                        CapNhatDSCT(ListCT);
                    }
                }
            }
            else
                MessageBox.Show("Bạn phải chọn một dòng thông tin để xóa!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            HoaDonMoi = null;
            Close();
        }

        private void frmBill_Load(object sender, EventArgs e)
        {
            cbTrangThai.Items.Add(BillState.DaNhanTB);
            cbTrangThai.Items.Add(BillState.DaSua);
            cbTrangThai.Items.Add(BillState.DaThanhToan);
            cbTrangThai.SelectedIndex = 0;
            if (!IsAdd)
            {
                txtTenTB.Text = HoaDonMoi.DeviceName;
                txtDiaChi.Text = HoaDonMoi.CustomerAddress;
                txtMaHD.Text = HoaDonMoi.ReceiptID;
                txtMoTaTB.Text = HoaDonMoi.DeviceDescription;
                txtTenKH.Text = HoaDonMoi.CustomerName;
                mtbSDT.Text = HoaDonMoi.PhoneNumber;
                dtpNgayNhan.Value = HoaDonMoi.DateReceiced;
                ListCT = HoaDonMoi.ListDetails;
                CapNhatDSCT(ListCT);
                cbTrangThai.Text = HoaDonMoi.Status;
                HoaDonMoi = null;
            }
            else
            {
                dtpNgayNhan.Value = DateTime.Today;
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            frmReceiptDetailPrint frm = new frmReceiptDetailPrint(ReceiptManager.GetSingle(txtMaHD.Text).Object);
            frm.ShowDialog();
        }
    }
}